
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ScrewTurn.Wiki.PluginFramework;
using System.Text;

namespace ScrewTurn.Wiki {

	public partial class History : BasePage {

        private Pages pages;
        private Users users;
        private PageInfo page;
        private PageContent content;

        protected void Page_Load(object sender, EventArgs e) {
            Page.Title = Resources.Messages.HistoryTitle + " - " + Settings.WikiTitle;

			pages = Pages.Instance;
			users = Users.Instance;
            page = pages.FindPage(Request["Page"]);

            if(page != null) {
                content = Content.GetPageContent(page, true);
                lblTitle.Text = Resources.Messages.PageHistory + ": " + content.Title;
            }
            else {
                lblTitle.Text = Resources.Messages.PageNotFound;
            }

			if(!IsPostBack && page != null) {
				List<int> revisions = Pages.Instance.GetBackups(page);
				revisions.Reverse();
				// Populate dropdown lists
				lstRev1.Items.Clear();
				lstRev2.Items.Clear();
				lstRev2.Items.Add(new ListItem(Resources.Messages.Current, "Current"));
				if(Request["Rev2"] != null && Request["Rev2"].Equals(lstRev2.Items[0].Value))
					lstRev2.SelectedIndex = 0;
				for(int i = 0; i < revisions.Count; i++) {
					lstRev1.Items.Add(new ListItem(revisions[i].ToString(), revisions[i].ToString()));
					lstRev2.Items.Add(new ListItem(revisions[i].ToString(), revisions[i].ToString()));
					if(Request["Rev1"] != null && Request["Rev1"].Equals(lstRev1.Items[i].Value))
						lstRev1.SelectedIndex = i;
					if(Request["Rev2"] != null && Request["Rev2"].Equals(lstRev2.Items[i + 1].Value))
						lstRev2.SelectedIndex = i + 1;
				}
				if(revisions.Count == 0) btnCompare.Enabled = false;
			}

			PrintHistory();
        }

        public void PrintHistory() {
            if(page == null) return;

			StringBuilder sb = new StringBuilder();

            if(Request["Revision"] == null) {
                // Show version list
				List<int> revisions = Pages.Instance.GetBackups(page);
				revisions.Reverse();

				PageContent c;

				sb.Append(@"<table id=""RevisionListTable"" cellpadding=""2"" cellspacing=""0"">");

				sb.Append(@"<tr id=""RevisionListHeader"">");
				sb.Append(@"<td style=""text-align: right;"">#</td>");
				sb.Append("<td><p><b>");
				sb.Append(Resources.Messages.Revision);
				sb.Append(@"</b></p></td><td><p><b>");
				sb.Append(Resources.Messages.RevisionComment);
				sb.Append(@"</b></p></td><td><p><b>");
				sb.Append(Resources.Messages.User);
				sb.Append("</b></p></td>");
				sb.Append("</tr>");

				c = Content.GetPageContent(page, true);
				sb.Append("<tr>");
				sb.Append(@"<td class=""revisionlistcelleven"">&nbsp;</td>");
				sb.Append(@"<td class=""revisionlistcelleven"">" + @"<a href=""");
				sb.Append(Tools.UrlEncode(page.Name));
				sb.Append(Settings.PageExtension);
				sb.Append(@"?NoRedirect=1""><b>");
				sb.Append(Resources.Messages.CurrentRevision);
				sb.Append("</b> - ");
				string shift = "";
				if(Request.Cookies[Settings.CultureCookieName] != null) shift = Request.Cookies[Settings.CultureCookieName]["T"];
				sb.Append(Tools.AlignWithPreferences(c.LastModified, shift).ToString(Settings.DateTimeFormat));
				sb.Append(@"</a></td><td class=""revisionlistcelleven""><small>");
				sb.Append(c.Comment.Length > 0 ? c.Comment : "&nbsp;");
				sb.Append(@"</small></td><td class=""revisionlistcelleven"">");
				sb.Append(Users.Instance.UserLink(c.User));
				sb.Append("</td>");
				sb.Append("</tr>");

				for(int i = 0; i < revisions.Count; i++) {
					c = Pages.Instance.GetBackupContent(page, revisions[i]);
					sb.Append("<tr>");
					string tmp = ((i + 1) % 2 == 0 ? "revisionlistcelleven" : "revisionlistcellodd");
					sb.Append(@"<td class=""");
					sb.Append(tmp);
					sb.Append(@""" style=""text-align: right;"">");
					sb.Append(revisions[i].ToString());
					sb.Append("</td>");
					sb.Append(@"<td class=""");
					sb.Append(tmp);
					sb.Append(@""">");
					sb.Append(@"<a href=""History.aspx?Page=");
					sb.Append(Tools.UrlEncode(page.Name));
					sb.Append("&amp;Revision=");
					sb.Append(revisions[i].ToString());
					sb.Append(@""">");
					sb.Append(Tools.AlignWithPreferences(c.LastModified, shift).ToString(Settings.DateTimeFormat));
					sb.Append(@"</a></td>");
					sb.Append(@"<td class=""");
					sb.Append(tmp);
					sb.Append(@"""><small>");
					sb.Append(c.Comment.Length > 0 ? c.Comment : "&nbsp;");
					sb.Append(@"</small></td><td class=""");
					sb.Append(tmp);
					sb.Append(@""">");
					sb.Append(Users.Instance.UserLink(c.User));
					sb.Append("</td>");
					sb.Append("</tr>");
				}

				sb.Append("</table>");
            }
            else {
				int rev = -1;
				try {
					rev = int.Parse(Request["Revision"]);
				}
				catch { }
				List<int> backups = Pages.Instance.GetBackups(page);
				if(!backups.Contains(rev)) {
					Response.Redirect(page.Name + Settings.PageExtension);
					return;
				}
				PageContent revision = Pages.Instance.GetBackupContent(page, rev);
				sb.Append(@"<table class=""box"" cellpadding=""0"" cellspacing=""0""><tr><td>");
				sb.Append(@"<p style=""text-align: center;""><b>");
				if(rev > 0) {
					sb.Append(@"<a href=""History.aspx?Page=");
					sb.Append(Tools.UrlEncode(page.Name));
					sb.Append(@"&amp;Revision=");
					sb.Append(Tools.GetVersionString((int)(rev - 1)));
					sb.Append(@""">&laquo; ");
					sb.Append(Resources.Messages.OlderRevision);
					sb.Append("</a>");
				}
				else {
					sb.Append("&laquo; ");
					sb.Append(Resources.Messages.OlderRevision);
				}

				sb.Append(@" - <a href=""History.aspx?Page=");
				sb.Append(Tools.UrlEncode(page.Name));
				sb.Append(@""">");
				sb.Append(Resources.Messages.BackToHistory);
				sb.Append("</a> - ");

				if(rev < backups.Count - 1) {
					sb.Append(@"<a href=""History.aspx?Page=");
					sb.Append(Tools.UrlEncode(page.Name));
					sb.Append(@"&amp;Revision=");
					sb.Append(Tools.GetVersionString((int)(rev + 1)));
					sb.Append(@""">");
					sb.Append(Resources.Messages.NewerRevision);
					sb.Append(" &raquo;</a>");
				}
				else {
					sb.Append(@"<a href=""");
					sb.Append(Tools.UrlEncode(page.Name));
					sb.Append(Settings.PageExtension);
					sb.Append(@""">");
					sb.Append(Resources.Messages.CurrentRevision);
					sb.Append("</a>");
				}
				sb.Append("</b></p></td></tr></table><br />");

				sb.Append(@"<h3 class=""separator"">");
				sb.Append(Resources.Messages.PageRevision);
				sb.Append(": ");
				string shift = "";
				if(Request.Cookies[Settings.CultureCookieName] != null) shift = Request.Cookies[Settings.CultureCookieName]["T"];
				sb.Append(Tools.AlignWithPreferences(revision.LastModified, shift).ToString(Settings.DateTimeFormat));
				sb.Append("</h3><br/ >");

				sb.Append(FormattingPipeline.FormatWithPhase3(FormattingPipeline.FormatWithPhase1And2(revision.Content, page).Replace("%%%%EditSectionPlaceHolder%%%%", ""), page));
            }

			lblHistory.Text = sb.ToString();
        }

		protected void btnCompare_Click(object sender, EventArgs e) {
			Response.Redirect("Diff.aspx?Page=" + Tools.UrlEncode(page.Name) + "&Rev1=" + lstRev1.SelectedValue + "&Rev2=" + lstRev2.SelectedValue);
		}

	}

}
